﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using Admin.CYT369.Com.WebBase;

namespace news.admin.cyt369.com.Areas.NewsAdmin.Controllers
{
    public partial class ArticlesController
    {
        [HttpGet]
        public JsonResult GetArticles()
        {
            int page = Request.Query.GetInteger("page", 0);
            int rows = Request.Query.GetInteger("rows", 20);
            int total = 0;

            string title = Request.Query.GetString("Title");
            var auditStatusId = Request.Query.GetInteger("AuditStatus");
            var contentTypeId = Request.Query.GetInteger("ContentTypeID");
            var isDeleted = Request.Query.GetBoolean("IsDeleted", false);


            var items = news.admin.cyt369.com.services.Services.ArticlesService.Instance.GetArticles(page, rows, ref total, 
                                                                                                     title, auditStatusId, contentTypeId, isDeleted).ToArray();
            foreach (var item in items)
            {
                if(item.DocExt != null)
                {
                    item.DocExt.DocContent = "";
                }
            }
            
            return DefaultJson(new { total = total, rows = items });
        }

        [HttpDelete]
        public IActionResult DeleteDocument(long id)
        {
            var resp = new Admin.CYT369.Com.WebBase.ExecuteResult();
            if (news.admin.cyt369.com.services.Services.ArticlesService.Instance.DeleteDocument(id))
            {
                resp.Result = true;
            }
            else
            {
                resp.Msg = "删除失败";
            }

            return DefaultJson(resp);
        }

        [HttpPost]
        public IActionResult DoArticleAudit()
        {
            var auditRemark = Request.Form.GetString("AuditRemark");
            var docId = Request.Form.GetLong("Id", 0);
            var result = Request.Form.GetBoolean("Result", false);
            var canComment = Request.Form.GetBoolean("CanComment", false);
            /// 是否审核通过: 0-未提交审核，1-通过审核，2-未通过审核, 10-提交审核
            if (news.admin.cyt369.com.services.Services.ArticlesService.Instance.DoArticleAudit(docId, canComment, (result ? 1 : 2), this.GetUserId(), auditRemark))
            {
                return Ok();
            }

            return BadRequest("保存审核失败");
        }

        [HttpGet]
        public JsonResult GetDocumentAudits(long id)
        {
            var items = news.admin.cyt369.com.services.Services.ArticlesService.Instance.GetDocumentAudits(id);

            return DefaultJson(items);
        }
    }
}